const connection = require('../database/dataBase')

class CouponService {
  // 获取所有优惠券
  async getCoupon(userId) {
    const queryUserCoupon = 'SELECT * FROM coupons WHERE userId = ?;'
    const result = await connection.execute(queryUserCoupon, [userId])
    return result[0]
  }
  // 添加优惠券
  async exchangeCoupon(CouponsData) {
    const { id, condition, reason, value, name, startAt, endAt, valueDesc, unitDesc, userId } = CouponsData
    const exchangeCoupon = 'INSERT INTO coupons (`id`, `condition`, `reason`, `value`, `name`, `startAt`, `endAt`, `valueDesc`, `unitDesc`, `userId`) VALUES (?,?,?,?,?,?,?,?,?,?)';
    await connection.execute(exchangeCoupon, [id, condition, reason, value, name, startAt, endAt, valueDesc, unitDesc, userId])
    return {
      status: 200,
      message: '兑换成功~'
    }
  }
  // 使用后删除优惠券
  async deleteCoupon(deleteInfo) {
    const { id, userId } = deleteInfo
    const deleteCoupon = 'DELETE FROM coupons WHERE `id` = ? && `userId` = ?;'
    await connection.execute(deleteCoupon, [id, userId])
    return {
      status: 200,
      message: '优惠券删除成功~'
    }
  }
}

module.exports = new CouponService()